package com.my.swas.sso.module.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.my.swas.common.utils.BeanCopierUtil;
import com.my.swas.sso.module.admin.entity.App;
import com.my.swas.sso.module.admin.service.AppService;
import com.my.swas.sso.module.cms.entity.CmsCategory;
import com.my.swas.sso.module.cms.mapper.CmsCategoryMapper;
import com.my.swas.sso.module.cms.service.CmsCategoryService;
import com.my.swas.sso.module.cms.vo.CategoryZTreeVo;
import com.my.swas.sso.module.cms.vo.LayTreeVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @author: :MaYong
 */
@Service
public class CmsCategoryServiceImpl extends ServiceImpl<CmsCategoryMapper,CmsCategory> implements CmsCategoryService {
    @Resource
    AppService appService;
    @Override
    public List<LayTreeVo> buildLayTree() {
        List<CmsCategory> list = getCurrentCategory(null);
        return buildTree(list,0l);
    }

    @Override
    public List<CmsCategory> getCurrentCategory(Long appId) {
        List<Long> appIdList;
        if(null != appId){
            appIdList = Lists.newArrayList(appId);
        }else {
            List<App> appList = appService.getAppByCurrentUser();
            appIdList = appList.stream().map(App::getId).collect(Collectors.toList());
        }
        CmsCategory cmsCategory = new CmsCategory();
        LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(cmsCategory)
                .in(CollectionUtils.isNotEmpty(appIdList),CmsCategory::getCategoryAppId,appIdList);
        return this.list(queryWrapper);
    }

    @Override
    public List<CategoryZTreeVo> buildZTree(Long appId) {
        List<CmsCategory> category = getCurrentCategory(appId);
        List<CategoryZTreeVo> list = Lists.newArrayList();
        category.forEach(cmsCategory -> {
            CategoryZTreeVo categoryZTreeVo = new CategoryZTreeVo();
            categoryZTreeVo.setId(cmsCategory.getCategoryId());
            categoryZTreeVo.setName(cmsCategory.getCategoryTitle());
            categoryZTreeVo.setPId(cmsCategory.getCategoryParentId());
            categoryZTreeVo.setOpen(Boolean.TRUE);
            list.add(categoryZTreeVo);
        });
        return list;
    }

    private List<LayTreeVo> buildTree(List<CmsCategory> cmsCategoryList,Long prentId){
        List<LayTreeVo> treeList = Lists.newArrayList();
        LayTreeVo layTreeVo;
        for (CmsCategory cmsCategory : cmsCategoryList) {
            if(cmsCategory.getCategoryParentId() == prentId){
                layTreeVo = new LayTreeVo();
                layTreeVo.setId(String.valueOf(cmsCategory.getCategoryId()));
                layTreeVo.setTitle(cmsCategory.getCategoryTitle());
                layTreeVo.setSpread(Boolean.TRUE);
                layTreeVo.setChildren(buildTree(cmsCategoryList,cmsCategory.getCategoryId()));
                treeList.add(layTreeVo);
            }
        }
        return treeList;
    }
}
